package com.feijia.aijingjingus;

import android.content.Context;
import android.content.Intent;
import android.graphics.Paint;
import android.os.Handler;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;

import org.json.JSONException;
import org.json.JSONObject;
import android.content.SharedPreferences;
import android.content.SharedPreferences.Editor;

import com.feijia.aijingjingus.util.PreferencesUtils;

import static com.feijia.aijingjingus.GlobalHelper.GUserNum;
import static com.feijia.aijingjingus.GlobalHelper.md5;

public class login_Activity extends AppCompatActivity {
    private EditText etPhoneNumber,etPassword;
    private String Password,PhoneNum;
    Context context =this;
    Handler handler = new Handler();
    private SharedPreferences sp;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        sp = this.getSharedPreferences("userInfo", Context.MODE_PRIVATE);
        setContentView(R.layout.activity_login_layout);
        Button button5 = (Button)findViewById(R.id.button5);
        button5.getPaint().setFlags(Paint.UNDERLINE_TEXT_FLAG);
        etPhoneNumber = (EditText) findViewById(R.id.etLoginPhoneNum);
        etPassword = (EditText) findViewById(R.id.etLoginPassword);
        if(!GUserNum.equals(""))
        {
            Intent intent =new Intent("controlCenterActivity");
            intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK|Intent.FLAG_ACTIVITY_CLEAR_TASK);
            startActivity(intent);

        }
    }
    public void onClickSignUp(View view)
    {
        startActivity(new Intent("signUp_Activity"));
    }

    public void onClickLogin(View view)
    {
        PreferencesUtils.putString(this,PreferencesUtils.PREFERENCE_NAME,"男");
        Password = etPassword.getText().toString().trim();
        Password = md5(Password);
        PhoneNum = etPhoneNumber.getText().toString().trim();
        if(Password.isEmpty()){
            Toast.makeText(getApplicationContext(), getString(R.string.PasswordNoEmpty),
                    Toast.LENGTH_SHORT).show();
            return;
        }

        if(PhoneNum.isEmpty()){
            Toast.makeText(getApplicationContext(), getString(R.string.PhoneNumberNoEmpty),
                    Toast.LENGTH_SHORT).show();
            return;
        }
        new Thread(new Runnable() {
            @Override
            public void run() {
                Login(PhoneNum,Password);
            }
        }).start();
    }

    public void onClickBack(View view)
    {
        finish();
    }
    private  void Login(String PhoneNum, String Password){
        JSONObject root= new JSONObject();
        try {
            root.put("UserNo", Long.parseLong(PhoneNum));
            root.put("UserPassword", Password);

        } catch (JSONException e) {
            e.printStackTrace();
        }
        JSONObject out = GlobalHelper.PostData(root,"http://120.77.255.57/ting/login.php");
        try {
            if (out.getInt("OperateResult")==1) {
                handler.post(new Runnable() {
                    @Override
                    public void run() {
                        Toast.makeText(context, getString(R.string.LoginSuccess), Toast.LENGTH_SHORT).show();
                    }
                });
                String OperateMsg = out.getString("OperateMsg"); // 获取操作结果信息
                System.out.printf(" OperateResult : %s \n OperateMsg : %s\n", out.getInt("OperateResult"), OperateMsg);
                GUserNum =PhoneNum;
                Editor editor = sp.edit();
                editor.putString("USER_NAME", PhoneNum);
                editor.apply();
                Intent intent =new Intent("controlCenterActivity");
                intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK|Intent.FLAG_ACTIVITY_CLEAR_TASK);
                startActivity(intent);
            } else {
                System.out.println("predict error");
                handler.post(new Runnable() {
                    @Override
                    public void run() {
                        Toast.makeText(context, getString(R.string.LoginFailed), Toast.LENGTH_SHORT).show();
                    }
                });
            }
        } catch (JSONException e) {
            e.printStackTrace();
        }
    }

    public void ForgetPassword(View view) {
//        Intent intent = new Intent();
//        intent.setClass(this,Smslogin.class);
//        startActivity(intent);
    }
}
